#ifndef _NETWORK_H
#define _NETWORK_H

#include "../CRRMCommon.h"

#include <string>
#include <vector>
#include <memory>
#include <mutex>
#include <list>
#include <thread>
using namespace DH;

class BoostNetWorkObject;
class DLL_DECL Network
{
public:
	Network(CRRMCommon* recv, unsigned short port);
	Network(CRRMCommon* recv, unsigned short port, std::string ip);
	~Network();

	static std::shared_ptr<Network> CreateTCPServer(unsigned short port, CRRMCommon* recv);
	static std::shared_ptr<Network> CreateTCPClient(std::string ip, unsigned short port, CRRMCommon* recv, unsigned short local_port = 0);
	static std::shared_ptr<Network> CreateUDPClient(std::string ip, unsigned short port, CRRMCommon* recv, unsigned short local_port = 0);
public:
	void SendRawData(void* data, size_t len, bool bTcpTransfer = true);
	void SendData(void* data, size_t len, DATATYPE dtype, bool bTcpTransfer = true);
	void BroadCastData(PACKAGE_INFO& data);
	unsigned short GetLocalSocketPort();
	std::list<unsigned short> GetRemoteSocketPort();
	std::list<std::string> GetSocketIP();

private:
	void UDPRecieve();
	void PackageProcess();
	bool ValidateUDP(PACKAGE_INFO& udp);
	void ResetByHeader(char* data, size_t len);

	bool m_is_exit;
	std::shared_ptr<BoostNetWorkObject> m_boost_object;

	CRRMCommon* mRecv;
	std::mutex m_mutex;
	std::vector<char> m_temp_buffer;
	std::list<std::vector<char>> m_pack_buffer;
};
#endif